﻿using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using SCResources.General;

namespace Serie_Counter.Forms
{
    public partial class SetAirdateForm : Form
    {
        private SerieAirInfo airInfo;

        //temp parameters to formcheck
        
        private int episode;
        private int season;
        private int frequency;
        private DateTime nextAirDate;

        public SerieAirInfo AirInfo
        {
            get
            {
                return airInfo;
            }
        }

        public SetAirdateForm(SerieAirInfo airInfo)
        {
            InitializeComponent();

            this.airInfo = airInfo;
            if(airInfo == null)
            {
                this.airInfo = new SerieAirInfo();
            }
            else
            {
                IniContent();
            }
        }

        private void IniContent()
        {
            textBoxCurSeason.Text = airInfo.Season.ToString();
            textBoxCurEpisode.Text = airInfo.Episode.ToString();
            textBoxAirDate.Text = airInfo.NextAirDate.Date.ToShortDateString();
            textBoxFrequency.Text = airInfo.Frequency.ToString();
        }

        private void buttonCalender_Click(object sender, EventArgs e)
        {
            ShowCalender();
        }

        /// <summary>
        /// Shows a dialog to select the next airdate, this is saved in nextAirDate para
        /// </summary>
        private void ShowCalender()
        {
            CalenderForm calenderForm = new CalenderForm();

            if (calenderForm.ShowDialog() == DialogResult.OK)
            {
                airInfo.NextAirDate = calenderForm.getSelectedDate();
                this.textBoxAirDate.Text = airInfo.NextAirDate.Date.ToShortDateString();
                calenderForm.Dispose();
            }
        }

        private void textBoxFrequency_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxFrequency.SelectAll();
        }

        private bool CheckErrors()
        {
            bool error = false;
            // ----- Air information ----
            //cur season
            if (Information.IsNumeric(textBoxCurSeason.Text))
            {
                season = int.Parse(textBoxCurSeason.Text);
                errorProvider.SetError(textBoxCurSeason, "");
            }
            else
            {
                error = true;
                errorProvider.SetError(textBoxCurSeason, "Enter a number");
            }
            //cur episode
            if (Information.IsNumeric(textBoxCurEpisode.Text))
            {
                episode = int.Parse(textBoxCurEpisode.Text);
                errorProvider.SetError(textBoxCurEpisode, "");
            }
            else
            {
                error = true;
                errorProvider.SetError(textBoxCurEpisode, "Enter a number");
            }

            //frequention
            if (!textBoxFrequency.Text.Equals("Ex. 7 (every week)"))
            {
                if (Information.IsNumeric(textBoxFrequency.Text))
                {
                    frequency = int.Parse(textBoxFrequency.Text);
                    errorProvider.SetError(textBoxFrequency, "");
                }
                else
                {
                    error = true;
                    errorProvider.SetError(textBoxFrequency, "Enter a number");
                }

            }
            else
            {
                frequency = 0;
            }

            return error;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if(!CheckErrors())
            {
                airInfo.Frequency = frequency;
                airInfo.Episode = episode;
                airInfo.Season = season;
                airInfo.NextAirDate = nextAirDate;

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
    }
}
